﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Analyses;
using PH.Model.Analyses.Bureaus;
using PH.Logic.Analyses;
namespace PH.Web.Management.Areas.Analyses.Controllers
{
    [RouteArea("Analyses")]
    [RoutePrefix("ANB")]
    public class ANBController : Controller
    {
        /// <summary>
        /// 根据路由判断是否是历史数据
        /// </summary>
        private bool History
        {
            get
            {
                var path = Request.Path;
                path = path.Replace("/Analyses/ANB/", "");
                return path.IndexOf("PB4") >= 0;
            }
        }

        [Route("RB4School")]
        [Route("PB4School")]
        public ActionResult B4School()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRB4School")]
        [Route("ExportPB4School")]
        public ActionResult ExportB4School()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("bGuid")))
                    {
                        BAnalysisQuery sea = new BAnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.BGuid = new Guid(Request.Form.Get("bGuid"));
                        sea.GradeLevel = int.Parse(Request.Form.Get("gradeLevel"));
                        sea.Key = Request.Form.Get("key");
                        sea.History = History;
                        var b4school = BureauLogic.GetB4School(sea);
                        if (b4school.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/b4school.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < b4school.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(b4school[i].SchoolCode);
                                cells[i + 4, 1].PutValue(b4school[i].SchoolName);
                                cells[i + 4, 2].PutValue(Model.Help.GetGradeLevelString(b4school[i].GradeLevel));
                                cells[i + 4, 3].PutValue(b4school[i].Samples);
                                cells[i + 4, 4].PutValue(b4school[i].Excellent);
                                cells[i + 4, 5].PutValue(b4school[i].ExcellentRate);
                                cells[i + 4, 6].PutValue(b4school[i].Good);
                                cells[i + 4, 7].PutValue(b4school[i].GoodRate);
                                cells[i + 4, 8].PutValue(b4school[i].Pass);
                                cells[i + 4, 9].PutValue(b4school[i].PassRate);
                                cells[i + 4, 10].PutValue(b4school[i].Fail);
                                cells[i + 4, 11].PutValue(b4school[i].FailRate);
                                cells[i + 4, 12].PutValue(b4school[i].Qualified);
                                cells[i + 4, 13].PutValue(b4school[i].QualifiedRate);
                                cells[i + 4, 14].PutValue(b4school[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "学校分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的机构编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RB4OriSchool")]
        [Route("PB4OriSchool")]
        public ActionResult B4OriSchool()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRB4OriSchool")]
        [Route("ExportPB4OriSchool")]
        public ActionResult ExportB4OriSchool()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("bGuid")))
                    {
                        BAnalysisQuery sea = new BAnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.BGuid = new Guid(Request.Form.Get("bGuid"));
                        sea.GradeLevel = int.Parse(Request.Form.Get("gradeLevel"));
                        sea.Key = Request.Form.Get("key");
                        sea.History = History;
                        var b4orischool = BureauLogic.GetB4OriSchool(sea);
                        if (b4orischool.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/b4orischool.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < b4orischool.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(b4orischool[i].SchoolCode);
                                cells[i + 4, 1].PutValue(b4orischool[i].SchoolName);
                                cells[i + 4, 2].PutValue(Model.Help.GetGradeLevelString(b4orischool[i].GradeLevel));
                                cells[i + 4, 3].PutValue(b4orischool[i].Samples);
                                cells[i + 4, 4].PutValue(b4orischool[i].Excellent);
                                cells[i + 4, 5].PutValue(b4orischool[i].ExcellentRate);
                                cells[i + 4, 6].PutValue(b4orischool[i].Good);
                                cells[i + 4, 7].PutValue(b4orischool[i].GoodRate);
                                cells[i + 4, 8].PutValue(b4orischool[i].Pass);
                                cells[i + 4, 9].PutValue(b4orischool[i].PassRate);
                                cells[i + 4, 10].PutValue(b4orischool[i].Fail);
                                cells[i + 4, 11].PutValue(b4orischool[i].FailRate);
                                cells[i + 4, 12].PutValue(b4orischool[i].Qualified);
                                cells[i + 4, 13].PutValue(b4orischool[i].QualifiedRate);
                                cells[i + 4, 14].PutValue(b4orischool[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "生源地分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的机构编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RB4Bureau")]
        [Route("PB4Bureau")]
        public ActionResult B4Bureau()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRB4Bureau")]
        [Route("ExportPB4Bureau")]
        public ActionResult ExportB4Bureau()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("bGuid")))
                    {
                        BAnalysisQuery sea = new BAnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.BGuid = new Guid(Request.Form.Get("bGuid"));
                        sea.GradeLevel = int.Parse(Request.Form.Get("gradeLevel"));
                        sea.Key = Request.Form.Get("key");
                        sea.History = History;
                        var b4bureau = BureauLogic.GetB4Bureau(sea);
                        if (b4bureau .TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/b4bureau.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < b4bureau .TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(b4bureau [i].Code);
                                cells[i + 4, 1].PutValue(b4bureau [i].EduName);
                                cells[i + 4, 2].PutValue(Model.Help.GetGradeLevelString(b4bureau [i].GradeLevel));
                                cells[i + 4, 3].PutValue(b4bureau [i].Samples);
                                cells[i + 4, 4].PutValue(b4bureau [i].Excellent);
                                cells[i + 4, 5].PutValue(b4bureau [i].ExcellentRate);
                                cells[i + 4, 6].PutValue(b4bureau [i].Good);
                                cells[i + 4, 7].PutValue(b4bureau [i].GoodRate);
                                cells[i + 4, 8].PutValue(b4bureau [i].Pass);
                                cells[i + 4, 9].PutValue(b4bureau [i].PassRate);
                                cells[i + 4, 10].PutValue(b4bureau [i].Fail);
                                cells[i + 4, 11].PutValue(b4bureau [i].FailRate);
                                cells[i + 4, 12].PutValue(b4bureau [i].Qualified);
                                cells[i + 4, 13].PutValue(b4bureau [i].QualifiedRate);
                                cells[i + 4, 14].PutValue(b4bureau [i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "机构分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的机构编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RB4Sample")]
        [Route("PB4Sample")]
        public ActionResult B4Sample()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("RB4Grade")]
        [Route("PB4Grade")]
        public ActionResult B4Grade()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRB4Grade")]
        [Route("ExportPB4Grade")]
        public ActionResult ExportB4Grade()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("bGuid")))
                    {
                        BAnalysisQuery sea = new BAnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.BGuid = new Guid(Request.Form.Get("bGuid"));
                        sea.Key = Request.Form.Get("key");
                        sea.History = History;
                        var b4grade = BureauLogic.GetB4Grade(sea);
                        if (b4grade.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/s4grade.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < b4grade.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(Model.Help.GetGradeName(b4grade[i].GradeCode));
                                cells[i + 4, 1].PutValue(b4grade[i].Samples);
                                cells[i + 4, 2].PutValue(b4grade[i].Excellent);
                                cells[i + 4, 3].PutValue(b4grade[i].ExcellentRate);
                                cells[i + 4, 4].PutValue(b4grade[i].Good);
                                cells[i + 4, 5].PutValue(b4grade[i].GoodRate);
                                cells[i + 4, 6].PutValue(b4grade[i].Pass);
                                cells[i + 4, 7].PutValue(b4grade[i].PassRate);
                                cells[i + 4, 8].PutValue(b4grade[i].Fail);
                                cells[i + 4, 9].PutValue(b4grade[i].FailRate);
                                cells[i + 4, 10].PutValue(b4grade[i].Qualified);
                                cells[i + 4, 11].PutValue(b4grade[i].QualifiedRate);
                                cells[i + 4, 12].PutValue(b4grade[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "年级分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的机构编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

    }
}
